package com.progroup.kyrsmoney.dialog;


import java.util.Random;

import org.w3c.dom.Text;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.progroup.checks.Checks;
import com.progroup.checks.q;
import com.progroup.kyrsmoney.R;

public class CustomDialog extends Dialog {
	LinearLayout view;
	ScrollView scroll;
	String text = null;
	int i=0;
	private int parametr;
	AnimationDrawable frameAnimation;
	public Button b_ok, b_cancel;
	
	
	
	public CustomDialog(Context context,  int parametr, String text) {
		super(context);
		// TODO Auto-generated constructor stub
		Checks.KeyBack=Checks.KEYBACK_NO;
		this.parametr = parametr;
		this.text = text;
	}
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
		if(parametr==Checks.DIALOG_LIST){
			
			setContentView(R.layout.custom_dialog_linearlayout);
			scroll = (ScrollView)findViewById(R.id.scrollView1);
			
			view = (LinearLayout)findViewById(R.id.custom_dialog_linearlayout_linear_scroll);
			
			int i = 0;
			do{
				view.addView(addBank(q.list_bank_info.get(i)[1].toString(), Boolean.parseBoolean(q.list_bank_info.get(i)[2].toString()), q.list_bank_info.get(i)[3].toString()));
				Log.i("LIST!!!!!!!!", i+"");
				i++;
			}while(i!=q.list_bank_info.size());
			
			
			b_ok = (Button)findViewById(R.id.dialog_button1);
			
			b_cancel = (Button)findViewById(R.id.dialog_button2);
			
			
		}else if(parametr==Checks.DIALOG_LOADING){
			/*setContentView(R.layout.custom_dialog_process);

			ImageView imageView = (ImageView)findViewById(R.id.imageView1);
			imageView.setBackgroundResource(R.drawable.anim_load);
			frameAnimation = (AnimationDrawable) imageView.getBackground();
			frameAnimation.start();*/
			setContentView(R.layout.custom_dialog_process);
			//Random rand = new Random();
			//int min = 0, max = 1;

			//int randomNum = rand.nextInt(max - min + 1) + min;
			
			ImageView image =(ImageView)findViewById(R.id.dialog_progress_imageView1);
			//image.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
			//if(randomNum==0){
			image.setBackgroundResource(R.drawable.anim_load);
			//}else{
				//image.setBackgroundResource(R.drawable.anim_progress);
			//}
			if(text!=null){
			TextView textview = (TextView)findViewById(R.id.dialog_progress_textView1);
			textview.setVisibility(View.VISIBLE);
			textview.setTextSize(14);
			}

			frameAnimation = (AnimationDrawable) image.getBackground();
			getWindow().setBackgroundDrawable(new ColorDrawable(0));
			//frameAnimation.start();
			
		}
		 
	}
	
	@Override
	
	  public void onWindowFocusChanged(boolean hasFocus) {
	
	    super.onWindowFocusChanged(hasFocus);
	    if(parametr==Checks.DIALOG_LOADING){
	    frameAnimation.start();
	    }
	
	  }

	
	public View addBank(String name, boolean checked, String imagelink){
		LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = new View(getContext());
		view = inflater.inflate(R.layout.custom_dialog_layout, null);
		ImageView image = (ImageView)view.findViewById(R.id.custom_dialog_image);
		image.setBackgroundResource(getContext().getResources().getIdentifier("drawable/"+imagelink, null, getContext().getPackageName()));
		final TextView name_bank = (TextView)view.findViewById(R.id.custom_dialog_name);
		name_bank.setText(name);
		CheckBox checkbox = (CheckBox)view.findViewById(R.id.custom_dialog_checkbox);
		checkbox.setChecked(checked);
		checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				q.TRUE_FALSE.put(name_bank.getText().toString(), isChecked);
			}
		});
		return view;
	}
	
	@Override
	public void dismiss() {
		// TODO Auto-generated method stub
		Checks.KeyBack=Checks.KEYBACK_YES;
		super.dismiss();
	}
	
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
  	  // TODO Auto-generated method stub
  	  if(Checks.KeyBack){
  		  
  		  return super.onKeyDown(keyCode, event);
  	  }else{
  		  return false;
  	  }
    }

	
    
	
}
